package com.stlm2.controller;

import com.stlm2.dems.card.service.CardService;
import com.stlm2.util.codecontent.CodeContent;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by apple on 2017/5/31.
 */
@Controller
@RequestMapping("/cardC")
public class CardController  {
    @Resource
    private CardService cardService;




    @ResponseBody
    @RequestMapping(value="/addCard.json",produces="application/json;charset=UTF-8")
    public String addCard(String companyCode,ModelMap model, String token, HttpServletResponse response, String bankId, String cardNum, String kaihuhang, String accountName){
        response.setCharacterEncoding("UTF-8");
        HashMap<String,Object> map=new HashMap<String, Object>();
        map= cardService.addCard(token,bankId,kaihuhang,accountName,cardNum);
        if("0".equals(map.get("code").toString())){
            model.put("code",0);
            model.put("msg",map.get("msg"));
            return new JSONObject(model).toString();

        }
        if("-1".equals(map.get("code").toString())){
            model.put("code",-1);
            model.put("msg",map.get("msg"));
            return new JSONObject(model).toString();
        }
        model.put("code",1);
        model.put("msg","添加银行卡成功");
        return new JSONObject(model).toString();
    }

    @ResponseBody
    @RequestMapping(value="/updateCard.json",produces="application/json;charset=UTF-8")
    public String updateCard(String companyCode,ModelMap model, String token, HttpServletResponse response, String bankId, String cardNum, String kaihuhang, String accountName,String cardId){
        response.setCharacterEncoding("UTF-8");
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            map= cardService.updateCard(token,bankId,kaihuhang,accountName,cardNum,cardId);
        }catch (Exception e){
            return CodeContent.getModelByCode(0,"编辑银行卡失败", model);
        }
        if("0".equals(map.get("code").toString())){
            model.put("code",0);
            model.put("msg",map.get("msg"));
            return new JSONObject(model).toString();

        }
        if("-1".equals(map.get("code").toString())){
            model.put("code",-1);
            model.put("msg",map.get("msg"));
            return new JSONObject(model).toString();
        }
        model.put("code",1);
        model.put("msg","编辑银行卡成功");
        return new JSONObject(model).toString();
    }

    /**
     * ffl
     * 2017-07-13 17:36:57
     * 获取用户的银行卡  列表
     * @param model
     * @param token
     * @param pageNu
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getCard.json",produces="application/json;charset=UTF-8")
    public String getCard(String companyCode,ModelMap model, String token,Integer pageNu, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
       String date;
        try {
            date= cardService.getCard(token);
        }catch (Exception e){
            e.getStackTrace();
            return CodeContent.getModelByCode(0,"获取银行卡信息失败", model);
        }
        return date;
    }

    /**
     * ffl
     * 2017-07-13 17:35:50
     * 获取指定id 的用户银行卡信息
     * @param model
     * @param token
     * @param cardId
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/getOneCard.json",produces="application/json;charset=UTF-8")
    public String getOneCard(String companyCode,ModelMap model, String token,Integer cardId, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        String date;
        try {
            date= cardService.getOneCard(token,cardId);
        }catch (Exception e){
            e.getStackTrace();
            return CodeContent.getModelByCode(0,"获取银行卡信息失败", model);
        }
        return date;
    }

    @ResponseBody
    @RequestMapping(value="/getCardList.json",produces="application/json;charset=UTF-8")
    public String getCardList(String companyCode,ModelMap model, String token,Integer pageNu, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        String date;

            date= cardService.getCardList(token,pageNu);

        return date;
    }

    @ResponseBody
    @RequestMapping(value="/unBindThisCard.json",produces="application/json;charset=UTF-8")
    public String unbindOneCard(String companyCode,ModelMap model, String token,Integer cardId, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        String date;
        try {
            date= cardService.unbindOneCard(token,cardId);
        }catch (Exception e){
            e.getStackTrace();
            return CodeContent.getModelByCode(0,"解绑银行卡失败", model);
        }
        return date;
    }

    @ResponseBody
    @RequestMapping(value="/toBeDefault.json",produces="application/json;charset=UTF-8")
    public String updateCardDefault(String companyCode,ModelMap model, String token,String cardId, HttpServletResponse response){
        response.setCharacterEncoding("UTF-8");
        String date;

            date= cardService.updateCardDefault(token,cardId);

        return date;
    }

}
